package abmb.pages;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import abmb.utils.*;

/**
 * Servlet implementation class Subscribe
 * @author Andrea Balboni
 * @version 1.0
 */
public class Subscribe extends HttpServlet {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Subscribe() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Utilities utils=new abmb.utils.Utilities();
		PageParts pp=new abmb.utils.PageParts();
		HttpSession session=request.getSession(true);
		response.setContentType("text/html");
		PrintWriter p= response.getWriter();
		p.println(pp.printHTMLHeader("Subscription",utils.isLoggedIn(session)));
		p.println("<section>");
		String FName,LName,NName,Pass;
		FName=request.getParameter("name");
		LName=request.getParameter("lastname");
		NName=request.getParameter("nickname");
		Pass=request.getParameter("passwd");
		
		if(!utils.checkUserRegistration(NName)){
			//user can signup
			utils.doSubscribe(FName, LName, NName, Pass, p);
		}else{//username already in use
			p.println("<p>Username "+NName+" already in use, please click <a href=\""+PageLinks.getSignupLink()+"\">here</a> and choose a different username</p>");
		}
		p.println("</section>");
		p.println(pp.printFooterSection());
		p.println(pp.printHTMLFooter());

	}


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
